package com.leoman.service.impl;

import com.leoman.core.Constant;
import com.leoman.dao.VideoDao;
import com.leoman.dao.VideoRecordDao;
import com.leoman.dao.VideoTypeDao;
import com.leoman.entity.Video;
import com.leoman.entity.VideoRecord;
import com.leoman.entity.VideoType;
import com.leoman.service.VideoTypeService;
import com.leoman.utils.QiNiuUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import java.util.List;

/**
 * VideoTypeServiceImpl
 * Created by 涂奕恒 on 2016/10/18 0018 下午 3:26.
 */
@Service
public class VideoTypeServiceImpl implements VideoTypeService {

    @Autowired
    private VideoTypeDao videoTypeDao;

    @Autowired
    private VideoDao videoDao;

    @Autowired
    private VideoRecordDao videoRecordDao;

    @Override
    public List<VideoType> findAll() {
        return videoTypeDao.findAll();
    }

    @Override
    public Page<VideoType> find(int pageNum, int pageSize) {
        return videoTypeDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<VideoType> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public VideoType getById(int id) {
        return videoTypeDao.findOne(id);
    }

    @Override
    public void deleteById(int id) {
        VideoType videoType = getById(id);

        if (null != videoType) {
            QiNiuUtils.delete(videoType.getAvatar());

            videoTypeDao.delete(id);
        }
    }

    @Override
    public VideoType create(VideoType videoType) {
        return videoTypeDao.save(videoType);
    }

    @Override
    public VideoType update(VideoType videoType) {
        return videoTypeDao.save(videoType);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public Integer saveVideoType(Integer videoTypeId, String name, MultipartRequest multipartRequest) {
        try {
            VideoType videoType;

            if (null == videoTypeId) {
                videoType = new VideoType();
                videoType.setCreateTime(System.currentTimeMillis());
                videoType.setUpdateTime(System.currentTimeMillis());
            } else {
                videoType = videoTypeDao.findOne(videoTypeId);
                videoType.setUpdateTime(System.currentTimeMillis());
            }

            if (StringUtils.isNotBlank(name)) {
                videoType.setName(name);
            }

            MultipartFile multipartFile = multipartRequest.getFile("avatar");
            if (null != multipartFile && multipartFile.getSize() > 0) {
                if (null != videoType.getId()) {
                    QiNiuUtils.delete(videoType.getAvatar());
                }
                videoType.setAvatar(QiNiuUtils.upload(multipartFile));
            }

            videoTypeDao.save(videoType);

            return videoType.getId();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Integer deleteVideoTypeId(Integer videoTypeId) {
        try {
            List<Video> videoList = videoDao.findListByVideoTypeId(videoTypeId);

            if (null != videoList && videoList.size() > 0) {
                return -1;
            }

            deleteById(videoTypeId);

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public List<VideoType> iFindList() {
        List<VideoType> list = videoTypeDao.findAll();
        List<Video> videoList;
        List<VideoRecord> videoRecordList;
        VideoRecord videoRecord;
        Integer status;
        Integer count;

        for (VideoType videoType : list) {
            videoList = videoDao.findListByVideoTypeIdDefault(videoType.getId());

            count = 0;
            for (Video video : videoList) {
                status = Constant.VIDEO_SHOW_STATUS_FOUR;

                videoRecordList = videoRecordDao.findListByVideoId(video.getId());
                videoRecord = videoRecordList.get(videoRecordList.size() - 1);

                if (videoRecord.getSendType().equals(Constant.VIDEO_SEND_TYPE_FREE)) {
                    if (videoRecord.getStartTime() > System.currentTimeMillis() || videoRecord.getEndTime() < System.currentTimeMillis()) {
                        status = Constant.VIDEO_SHOW_STATUS_SIX;
                    }
                }

                if (status.equals(Constant.VIDEO_SHOW_STATUS_FOUR)) {
                    count++;
                }
            }

            videoType.setVideoNum(count);
        }

        return list;
    }
}